<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="账单类型" prop="payfBillType">
        <el-select v-model="queryParams.payfBillType" placeholder="请选择账单类型" clearable>
          <el-option
            v-for="dict in dict.type.bill_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="款项类型" prop="payfPaymentType">
        <el-select v-model="queryParams.payfPaymentType" placeholder="请选择账单类型" clearable>
          <el-option
            v-for="dict in dict.type.payment_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合同编号" prop="payfContractCode">
        <el-input
          v-model="queryParams.payfContractCode"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="payfCustomerName">
        <el-input
          v-model="queryParams.payfCustomerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付时间" prop="payfPaymentDate">
        <el-date-picker clearable
          v-model="queryParams.payfPaymentDate"
          type="date"
          style="width: 205px"
          value-format="yyyy-MM-dd"
          placeholder="请选择支付时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:payfeesInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:payfeesInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:payfeesInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:payfeesInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="payfeesInfoList" @selection-change="handleSelectionChange" @row-click="handleRowClick">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合同编号" width="180" align="center" prop="payfContractCode" />
      <el-table-column label="客户名称" show-overflow-tooltip align="center" prop="payfCustomerName" />
      <el-table-column label="支付金额" width="180" align="center" prop="payfAmount" />
      <el-table-column label="支付时间" align="center" prop="payfPaymentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payfPaymentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账单类型" align="center" width="120" prop="payfBillType">
      <template slot-scope="scope">
        <dict-tag :options="dict.type.bill_type" :value="scope.row.payfBillType"/>
      </template>
    </el-table-column>
      <el-table-column label="缴费类型" width="180" align="center" prop="payfPaymentType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.payment_type" :value="scope.row.payfPaymentType ? scope.row.payfPaymentType.split(',') : []"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="180px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            title="修改"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:payfeesInfo:edit']"
          ></el-button>
          <el-button
            size="small"
            type="danger"
            title="删除"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:payfeesInfo:remove']"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改租金缴费统计对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="账单类型" prop="payfBillType">
              <el-radio-group v-model="form.payfBillType">
                <el-radio
                  v-for="dict in dict.type.bill_type"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="款项类型" prop="payfPaymentType">
              <el-select style="width: 100%;" v-model="form.payfPaymentType" multiple placeholder="请选择缴费款项类型" clearable>
                <el-option
                  v-for="dict in dict.type.payment_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="缴费总金额" prop="payfAmount">
              <el-input v-model.number="form.payfAmount" type="number" clearable placeholder="请输入缴费总金额" >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="关联合同" prop="payfContractId">
          <el-select style="width: 100%;" @change="changeContract" filterable v-model="form.payfContractId" placeholder="请选择缴费款项类型" clearable>
            <el-option
              v-for="contract in contractList"
              :key="contract.contractId"
              :label="contract.contractCode  + '-' + contract.contractAssetName + '-' + contract.contractCustomerName"
              :value="contract.contractId"
            />
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="支付时间" prop="payfPaymentDate">
              <el-date-picker clearable
                              style="width: 100%;"
                              v-model="form.payfPaymentDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择缴费支付时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="起止时间" prop="payfDate">
              <el-date-picker
                clearable
                style="width: 100%"
                v-model="form.payfDate"
                type="daterange"
                value-format="yyyy-MM-dd"
                @change="handleDateChange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="payfRemark">
          <el-input rows="3" v-model="form.payfRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="支付凭证" prop="payfPaymentUrl">
          <image-upload :limit="2" v-model="form.payfPaymentUrl"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPayfeesInfo, getPayfeesInfo, delPayfeesInfo, addPayfeesInfo, updatePayfeesInfo } from "@/api/business/payfeesInfo";
import { listContractAll } from "@/api/business/contractInfo";
export default {
  name: "PayfeesInfo",
  dicts: ['bill_type', 'payment_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租金缴费统计表格数据
      payfeesInfoList: [],
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        payfContractId: null,
        payfContractCode: null,
        payfCustomerId: null,
        payfCustomerName: null,
        payfBillType: null,
        payfPaymentType: null,
        payfAmount: null,
        payfStartDate: null,
        payfEndDate: null,
        payfPaymentDate: null,
        payfRemark: null,
        payfPaymentUrl: null,
        payfIsDelete: null,
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        payfContractId: [
          { required: true, message: "请选择关联合同", trigger: "change" }
        ],
        payfBillType: [
          { required: true, message: "请选择账单类型", trigger: "change" }
        ],
        payfPaymentType: [
          { required: true, message: "请选择款项类型", trigger: "change" }
        ],
        payfAmount: [
          { required: true, message: "缴费总金额不能为空", trigger: "blur" },
          { type: 'number', message: '请输入数字', trigger: 'change' },
          { validator: (rule, value, callback) => {
              if (value && value <= 0) {
                callback(new Error('缴费总金额必须大于 0'));
              } else {
                callback();
              }
            }, trigger: 'change'
          }
        ],
        payfDate: [
          { required: true, message: "起止时间不能为空", trigger: "blur" }
        ],
        payfEndDate: [
          { required: true, message: "费用周期止不能为空", trigger: "blur" }
        ],
        payfPaymentDate: [
          { required: true, message: "支付时间不能为空", trigger: "blur" }
        ],
        payfPaymentUrl: [
          { required: true, message: "支付凭证不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    listContractAll().then(response => {
      this.contractList = response.rows;
    });
    this.getList();
  },
  methods: {
    // 行点击
    handleRowClick(row, column, event) {
      if(column.label != '操作') {
        this.$router.push('/PayFeesLedger/payfees-info/' + row.payfId)
      }
    },
    changeContract(val) {
      if(val) {
        let item = this.contractList.find(item => item.contractId == val)
        this.form.payfCustomerId = item.contractCustomerId
        this.form.payfCustomerName = item.contractCustomerName
        this.form.payfContractCode = item.contractCode
      } else {
        this.form.payfCustomerId = ''
        this.form.payfCustomerName = ''
        this.form.payfContractCode = ''
      }
    },
    handleDateChange(val) {
     if(val) {
       this.form.payfStartDate = val[0]
       this.form.payfEndDate = val[1]
     } else {
       this.form.payfStartDate = ''
       this.form.payfEndDate = ''
     }
    },
    /** 查询租金缴费统计列表 */
    getList() {
      this.loading = true;
      listPayfeesInfo(this.queryParams).then(response => {
        this.payfeesInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        payfId: null,
        payfContractId: null,
        payfContractCode: null,
        payfCustomerId: null,
        payfCustomerName: null,
        payfBillType: '0',
        payfPaymentType: [],
        payfAmount: null,
        payfStartDate: null,
        payfEndDate: null,
        payfPaymentDate: null,
        payfRemark: null,
        payfPaymentUrl: null,
        payfDate: [],
        payfIsDelete: null,
        delFlag: null,
        deptId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.payfId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租金缴费统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const payfId = row.payfId || this.ids
      getPayfeesInfo(payfId).then(response => {
        this.form = response.data;
        this.form.payfPaymentType = this.form.payfPaymentType.split(",");
        this.form.payfBillType = this.form.payfBillType.toString();
        this.form.payfDate = [this.form.payfStartDate, this.form.payfEndDate]
        this.open = true;
        this.title = "修改租金缴费统计";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          delete this.form.payfDate
          this.form.payfPaymentType = this.form.payfPaymentType.join(",");
          if (this.form.payfId != null) {
            updatePayfeesInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPayfeesInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const payfIds = row.payfId || this.ids;
      this.$modal.confirm('是否确认删除租金缴费统计编号为"' + payfIds + '"的数据项？').then(function() {
        return delPayfeesInfo(payfIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/payfeesInfo/export', {
        ...this.queryParams
      }, `payfeesInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
